package com.boang.service.impl;

import com.boang.entity.Film;
import com.boang.service.FilmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Types;
import java.util.List;

@Service
public class FilmServiceImpl implements FilmService {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public Film selectById(int id) {
        String sql = "select * from film where film_id = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Film.class), id);
        } catch (EmptyResultDataAccessException e) {
            // 处理没有找到结果的情况
            return null; // 或者抛出自定义异常
        }
    }

    @Override
    public List<Film> selectAll() {
        String sql = "select * from film";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Film.class));
    }

    @Override
    public int insert(Film film) {
        String sql = "INSERT INTO film (title, description, release_year, language_id, " +
                "original_language_id, rental_duration, rental_rate, length, " +
                "replacement_cost, rating, special_features, last_update) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now())";

        return  jdbcTemplate.update(sql, ps -> { //preparedStatement
            ps.setString(1, film.getTitle());
            ps.setString(2, film.getDescription());
            ps.setObject(3, film.getReleaseYear());
            ps.setInt(4, film.getLanguageId());
            ps.setObject(5, film.getOriginalLanguageId());
            ps.setInt(6, film.getRentalDuration());
            ps.setDouble(7, film.getRentalRate());
            ps.setObject(8, film.getLength());
            ps.setDouble(9, film.getReplacementCost());
            ps.setString(10, film.getRating());
            ps.setString(11, film.getSpecialFeatures());
        });
    }

    @Override
    public int update(Film film) {
        String sql = "UPDATE film SET title = ?, description = ?, release_year = ?, " +
                "language_id = ?, original_language_id = ?, rental_duration = ?, " +
                "rental_rate = ?, length = ?, replacement_cost = ?, rating = ?, " +
                "special_features = ? WHERE film_id = ?";

        // 使用jdbcTemplate进行更新操作
        return jdbcTemplate.update(sql, ps -> {
            ps.setString(1, film.getTitle());
            ps.setString(2, film.getDescription());
            ps.setObject(3, film.getReleaseYear(), Types.INTEGER);
            ps.setInt(4, film.getLanguageId());
            ps.setObject(5, film.getOriginalLanguageId(), Types.INTEGER);
            ps.setInt(6, film.getRentalDuration());
            ps.setDouble(7, film.getRentalRate());
            ps.setObject(8, film.getLength(), Types.INTEGER);
            ps.setDouble(9, film.getReplacementCost());
            ps.setString(10, film.getRating());
            ps.setString(11, film.getSpecialFeatures());
            ps.setInt(12, film.getFilmId());
        });
    }
    @Override
    public int delete(int filmId) {
        String sql = "DELETE FROM film WHERE film_id = ?";

        return jdbcTemplate.update(sql, filmId);
    }
}
